package api

import (
	"net/http"
	"fmt"
	"io/ioutil"
	"encoding/json"
	"go-web/model"
	"go-web/web"
)

func IndexHandle() web.WebHandler {
	return func(writer http.ResponseWriter, request *http.Request) {
		writer.Header().Set("Content-Type", "application/json")
		request.ParseForm()
		method := request.Method

		rps,err := json.Marshal(ERROR)
		if method == GET {
			values := request.Form
			fmt.Println(values)
			if err == nil {
				user := model.User{}
				users := user.Query()
				data,error := json.Marshal(users)
				if error == nil {
					writer.Write([]byte(data))
				} else {
					writer.Write([]byte(rps))
				}
			} else {
				fmt.Println(rps)
			}
		} else if method == POST {
			data,err := ioutil.ReadAll(request.Body)
			if err != nil {
				writer.Write([]byte(rps))
				return
			}
			writer.Write(data)
		}
	}
}
